from flask import jsonify, Blueprint
from flask_restful import Api, Resource, reqparse

from models.model import *

vendor_bp = Blueprint('vendor_bp', __name__)
api = Api(vendor_bp)


class VendorView(Resource):
    def get(self):
        req = reqparse.RequestParser()
        req.add_argument('name')
        req.add_argument('linkman')
        req.add_argument('mobile')
        req.add_argument('page', type=int)
        req.add_argument('per_page', type=int)
        args = req.parse_args()
        name = args['name']
        linkman = args['linkman']
        mobile = args['mobile']
        page = args['page']
        limit = args['per_page']
        sql = Vendor.query
        if name:
            sql = sql.filter(Vendor.name.like(f"%{name}%"))
        if linkman:
            sql = sql.filter(Vendor.linkman.like(f"%{linkman}%"))
        if mobile:
            sql = sql.filter(Vendor.mobile.like(f"%{mobile}%"))
        if not page:
            page = 1
        if not limit:
            limit = 5
        vendor_remark = sql.paginate(page, limit)
        vendor_list = []
        for i in vendor_remark.items:
            a = {
                'id': i.id,
                'name': i.name,
                'linkman': i.linkman,
                'mobile': i.mobile,
                'remark': i.remark
            }
            vendor_list.append(a)
        return jsonify({
            'code': 200,
            'msg': '获取成功',
            'vendor_list': vendor_list
        })

    def post(self):
        req = reqparse.RequestParser()
        req.add_argument('name', required=True)
        req.add_argument('linkman', required=True)
        req.add_argument('mobile', required=True)
        req.add_argument('remark', required=True)
        args = req.parse_args()
        try:
            db.session.add(Vendor(**args))
            db.session.commit()
        except:
            return jsonify({
                'code': 400,
                'msg': '添加失败'
            })
        return jsonify({
            'code': 200,
            'msg': '添加成功'
        })


class VendorOneView(Resource):
    def get(self, id):
        vendor_one = Vendor.query.filter(Vendor.id == id).first()
        if not vendor_one:
            return jsonify({
                'code': 400,
                'msg': '供货商不存在'
            })
        vendor_list = {
            'id': vendor_one.id,
            'name': vendor_one.name,
            'linkman': vendor_one.linkman,
            'mobile': vendor_one.mobile,
            'remark': vendor_one.remark
        }
        return jsonify({
            'code': 200,
            'msg': "获取成功",
            'vendor_list': vendor_list
        })

    def put(self, id):
        req = reqparse.RequestParser()
        req.add_argument('name', required=True)
        req.add_argument('linkman', required=True)
        req.add_argument('mobile', required=True)
        req.add_argument('remark', required=True)
        args = req.parse_args()
        vendor_one = Vendor.query.filter(Vendor.id == id).first()
        if not vendor_one:
            return jsonify({
                'code': 400,
                'msg': '供货商不存在'
            })
        try:
            Vendor.query.filter(Vendor.id == id).update(args)
            db.session.commit()
            return jsonify({
                'code': 200,
                'msg': '更新成功'
            })
        except:
            return jsonify({
                'code': 400,
                'msg': '更新失败'
            })

    def delete(self, id):
        vendor_one = Vendor.query.filter(Vendor.id == id).first()
        if not vendor_one:
            return jsonify({
                'code': 400,
                'msg': '供货商不存在'
            })
        Vendor.query.filter(Vendor.id == id).delete()
        db.session.commit()
        return jsonify({
            'code': 200,
            'msg': '删除成功'
        })


api.add_resource(VendorView, '/v2/supplier')
api.add_resource(VendorOneView, '/supplier/<int:id>')
